/**
 * 删除链表的倒数第N个节点
 * https://leetcode.cn/problems/remove-nth-node-from-end-of-list/?envType=study-plan-v2&envId=top-100-liked
 */

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null) {
            return head;
        }
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        //统计长度
        int x = count - n;//计算要走的步数
        if(x < 0) {
            return head;//如果x小于0 证明删除节点不合法
        }
        cur = head;
        while(x != 1) {
            
            cur = cur.next;
        } 
        cur.next = cur.next.next;
        return head;
    }
}